{% block tinymce_script %}
	{% if TINYMCE_JS_URL %}
		<script type="text/javascript" src="{{ TINYMCE_JS_URL }}"></script>
	{% endif %}
{% endblock %}

{% block tinymce_init %}
	<script type="text/javascript">
		{% block functions %}{% endblock %}
    {% if TINYMCE_DOMAIN %}
      document.domain = '{{ TINYMCE_DOMAIN }}';
    {% endif %}

    (function($){
        var tinymce_added = {};
        var tinymce_options = {
{% block settings %}
            height: '300',
            {% if TINYMCE_CONTENT_CSS_URL %}content_css: "{{ TINYMCE_CONTENT_CSS_URL }}",{% endif %}
            {% if TINYMCE_LINK_LIST_URL %}link_list: "{{ TINYMCE_LINK_LIST_URL }}",{% endif %}
            plugins: [
			"advlist autolink autosave link image lists charmap print preview hr anchor pagebreak spellchecker",
			"searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking",
			"save table contextmenu directionality emoticons template textcolor paste fullpage textcolor colorpicker"
		],
		external_plugins: {
			//"moxiemanager": "/moxiemanager-php/plugin.js"
		},content_css: "css/development.css",
		add_unload_trigger: false,
		autosave_ask_before_unload: false,
		toolbar1: "save newdocument fullpage | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | styleselect formatselect fontselect fontsizeselect",
		toolbar2: "cut copy paste pastetext | searchreplace | bullist numlist | outdent indent blockquote | undo redo | link unlink anchor image media help | insertdatetime preview | forecolor backcolor",
		toolbar3: "table | hr removeformat | subscript superscript | charmap emoticons | print fullscreen | ltr rtl | spellchecker | visualchars visualblocks nonbreaking template pagebreak restoredraft | insertfile insertimage code",
		menubar: false,
		toolbar_items_size: 'small',
            paste_auto_cleanup_on_paste: true,
            relative_urls: false,
            invalid_elements: 'script',
            statusbar: false,
            menubar : false,
{% endblock %}
			language:'zh_CN',
        };

        function feincms_richtext_remove_tinymce(field) {
            var id = field ? field.id : this.id;
            if(tinymce_added[id]) {
                tinyMCE.execCommand('mceRemoveEditor', false, id)
                tinymce_added[id] = false;
            }
        }

        function feincms_richtext_add_tinymce(field) {
            var id = field ? field.id : this.id;
            if(!tinymce_added[id]) {
                tinyMCE.execCommand('mceAddEditor', false, id)
                tinymce_added[id] = true;
                }
        }

        var richtext_init_fn = function(){
            tinyMCE.init(tinymce_options);
            $('{% block selectors %}.order-machine textarea.item-richtext, #frontend_editor textarea.item-richtext{% endblock %}').each(function(){
                feincms_richtext_add_tinymce(this);
            });
        }

			{% block enable %}
        contentblock_init_handlers.push(richtext_init_fn);
        contentblock_move_handlers.poorify.push(function(item) {
            try {
                tinyMCE.triggerSave();
            } catch (error) {
                alert("tinymce failed: " + error);
            }
            item.find('textarea.item-richtext').each(feincms_richtext_remove_tinymce);
        });
        contentblock_move_handlers.richify.push(function(item) {
            item.find('textarea.item-richtext').each(feincms_richtext_add_tinymce);
        });
			{% endblock %}
    })(feincms.jQuery);
	
	</script>
{% endblock %}
